package com.phyane.home.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.phyane.home.param.ContentParam;
import com.phyane.home.service.SendNewService;

public class SendNewAction extends ActionSupport{
	
	private static final long serialVersionUID = -2316747782970002510L;
	
	private String content ;  
    private File file ; //具体上传文件的 引用 , 指向临时目录中的临时文件  
    private String fileFileName ;  // 上传文件的名字 ,FileName 固定的写法  
    private String fileContentType ; //上传文件的类型， ContentType 固定的写法  
      
    @SuppressWarnings("deprecation")
	@Override  
    public String execute() throws Exception {
    	//get new current time
    	ContentParam param = new ContentParam();
    	Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssS");
		System.out.println(df.format(date));
		String name = df.format(date) + fileFileName.substring(fileFileName.lastIndexOf("."));
        //获取文件存储路径  
        String authod = "admin";
		String path = ServletActionContext.getRequest().getRealPath("/userData/"+authod);
		name ="/phyane/userData/"+authod+"/"+name;
		param.setAuthod(1);
		param.setContent(content);
		param.setImage(name);
        System.out.println(System.getProperty("user.dir"));
        System.out.println(System.getProperty("java.io.tmpdir"));
        System.out.println(path);
        File uploadDir = new File(path);
        if(!uploadDir.exists()){
        	uploadDir.mkdirs();
        }
        //输出流  
        File toWrite = new File(path,fileFileName);
        OutputStream os = new FileOutputStream(toWrite);  
        //输入流  
        InputStream is = new FileInputStream(file);  
          
        byte[] buf = new byte[1024];  
        int length = 0 ;  
          
        while(-1 != (length = is.read(buf))){  
            os.write(buf, 0, length) ;  
        }
        is.close();
        os.close();
        String dPt = toWrite.getParent()+"\\"+name.substring(name.lastIndexOf("/")+1);
        System.out.println(dPt);
        File desert = new File(dPt);
        System.out.println(toWrite.renameTo(desert));
        new SendNewService().writeData(param);
        return SUCCESS; 
    }
    
    public void addActionError(String anErrorMessage) {
    	//这里要先判断一下，是我们要替换的错误，才处理
    	System.out.println(anErrorMessage);
    	if (anErrorMessage.startsWith("the request was rejected because its size")) {
    		Matcher m = Pattern.compile("\\d+").matcher(anErrorMessage);
    		String s1 = "";
    		if (m.find())
    			s1 = m.group();
    		String s2 = "";
    		if (m.find())
    			s2 = m.group();
    		//偷梁换柱，将信息替换掉       
		   super.addActionError("你上传的文件大小（" + s1 + "）超过允许的大小（" + s2 + "）");
		   //也可以改为在Field级别的错误
		   // super.addFieldError("file","你上传的文件大小（" + s1 + "）超过允许的大小（" + s2 + "）");
    	} else {
    		//否则按原来的方法处理
    		super.addActionError(anErrorMessage);
    	}
	}     
    	   
    public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public File getFile() {  
        return file;  
    }  
    public void setFile(File file) {  
        this.file = file;  
    }  
    public String getFileFileName() {  
        return fileFileName;  
    }  
    public void setFileFileName(String fileFileName) {  
        this.fileFileName = fileFileName;  
    }  
    public String getFileContentType() {  
        return fileContentType;  
    }  
    public void setFileContentType(String fileContentType) {  
        this.fileContentType = fileContentType;  
    }  
      
    

}
